//
//  MineBenefitsController.swift
//  SCA
//
//  Created by tjun on 2025/9/23.
//

import UIKit

class MineBenefitsController: BaseController {
    
    private var datas: [MineBenefitsModel] = []
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = AppStyle.color.background
        tableView.rowHeight = 40
        tableView.contentInsetAdjustmentBehavior = .never;
        tableView.register(MineBenefitsCell.self, forCellReuseIdentifier: MineBenefitsCell.identifier)
        tableView.estimatedRowHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.estimatedSectionHeaderHeight = 0
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: tabBarSafeBottomMargin, right: 0)
        tableView.separatorStyle = .none
        return tableView
    }()
    
    private lazy var emptyDataHandler = EmptyDataHandler(scrollView: tableView, configuration: { model in
        model.text = "暂无数据".localizedString
        return .onlyText
    })
    
    
    private let purchaseQualificationView = StepProgressView()
    private let recommendedQualificationsView = StepProgressView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        purchaseQualificationView.titleLabel.text = "自购资格".localizedString
        purchaseQualificationView.currentStep = 0
        purchaseQualificationView.addTo(view)
        purchaseQualificationView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.top.equalTo(navBarHeight)
            make.height.equalTo(90)
        }
        
        recommendedQualificationsView.titleLabel.text = "推荐资格".localizedString
        recommendedQualificationsView.currentStep = 0
        recommendedQualificationsView.addTo(view)
        recommendedQualificationsView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.top.equalTo(purchaseQualificationView.snp.bottom)
            make.height.equalTo(purchaseQualificationView)
        }
        
        let benefitsView = MineBenefitsView().addTo(self.view)
        benefitsView.dateLabel.text = "日期".localizedString
        benefitsView.prizeLabel.text = "奖品".localizedString
        benefitsView.stateLabel.text = "状态".localizedString
        benefitsView.operateButton.setTitle("操作".localizedString, for: .normal)
        benefitsView.operateButton.isUserInteractionEnabled = false
        benefitsView.dateLabel.font = UIFontMediumMake(14)
        benefitsView.prizeLabel.font = UIFontMediumMake(14)
        benefitsView.stateLabel.font = UIFontMediumMake(14)
        benefitsView.operateButton.titleLabel?.font = UIFontMediumMake(14)

        benefitsView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.top.equalTo(recommendedQualificationsView.snp.bottom).offset(8)
            make.height.equalTo(40)
        }
        
        let lineView = UIView().addTo(self.view)
        lineView.backgroundColor = .white.withAlphaComponent(0.3)
        lineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.top.equalTo(benefitsView.snp.bottom)
            make.height.equalTo(1)
        }
        
        tableView.addTo(view)
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(lineView.snp.bottom)
        }
        
        loadData()
    }
 
    func loadData() {
        MineService.loadMineBenefitsData(success: { benefitsInfo in
            self.datas.removeAll()
            self.datas.append(contentsOf: benefitsInfo.models)
            self.tableView.reloadData()
            self.purchaseQualificationView.currentStep = benefitsInfo.ref
            self.recommendedQualificationsView.currentStep = benefitsInfo.mySelf

        }) { error in
            
        }
    }
}


extension MineBenefitsController: UITableViewDataSource, UITableViewDelegate {
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MineBenefitsCell.identifier, for: indexPath) as! MineBenefitsCell
        cell.indexPath = indexPath
        let model = datas[indexPath.row]
        cell.model = model
        
        cell.onOperateButtonTapped = { [weak self] in
            guard let weakSelf = self else { return }
            
            let conversionController = ConversionController(Id: model.Id)
            weakSelf.navigationController?.pushViewController(conversionController, animated: true)
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        
    }
    
}
